#!/usr/bin/python
# coding=utf-8

import sys, datetime, json, logging

from xpy3lib.utils import db_utils as util

from sicost.config import app_config
from xpy3lib.XLogger import XLogger
from xpy3lib.utils.XUtils import XUtils

from sicost.BZBZFYJob import BZBZFYJob

profile = 'dev'


def bzbzfy(account, month):
    status = 0
    try:
        info = XUtils.get_git_info()
    except Exception as e:
        info = {'message': 'no_git'}

    level = logging.ERROR if profile == 'prod' else logging.NOTSET
    XLogger.getInstance(p_level=level, p_name_key_word='sicost').logger.error(
        '\n%s' % json.dumps(info, sort_keys=True, indent=4))

    config = app_config[profile]



    try:
        db_conn_mpp = util.getConnectionDb2(config.DB_HOST_MPP_DB2,
                                            config.DB_PORT_MPP_DB2,
                                            config.DB_DBNAME_MPP_DB2,
                                            config.DB_USER_MPP_DB2,
                                            config.DB_PASSWORD_MPP_DB2)
        XLogger.getInstance().logger.info('connect db_conn_mpp success')

        db_conn_dbprod7 = util.getConnectionDb2(config.DB_HOST_DBPROD7_DB2,
                                                config.DB_PORT_DBPROD7_DB2,
                                                config.DB_DBNAME_DBPROD7_DB2,
                                                config.DB_USER_DBPROD7_DB2,
                                                config.DB_PASSWORD_DBPROD7_DB2)
        XLogger.getInstance().logger.info('connect db_conn_dbprod7 success')
        db_conn_rds = util.getConnectionDb2(config.DB_HOST_RDS_DB2,
                                            config.DB_PORT_RDS_DB2,
                                            config.DB_DBNAME_RDS_DB2,
                                            config.DB_USER_RDS_DB2,
                                            config.DB_PASSWORD_RDS_DB2)
        XLogger.getInstance().logger.info('connect db_conn_rds success')
    except Exception as e:
        db_conn_mpp = None
        db_conn_rds = None
        db_conn_dbprod7 = None
        XLogger.getInstance().logger.critical(str(e))

    try:
        run_bzbzfy(p_config=config,
                       p_db_conn_mpp=db_conn_mpp,
                       p_db_conn_rds=db_conn_rds,
                       p_db_conn_dbprod7=db_conn_dbprod7,
                       p_account=account,
                       p_month=month
                       )
    except Exception as e:
        XLogger.getInstance().logger.critical(str(e))
    # run_bzbzfy(p_config=config, p_db_conn_mpp=db_conn_mpp, p_db_conn_rds=db_conn_rds, p_db_conn_dbprod7=db_conn_dbprod7,
    #             p_account=account, p_month=month)

    try:
        util.closeConnection(db_conn_mpp)
        util.closeConnection(db_conn_rds)
        util.closeConnection(db_conn_dbprod7)
    except Exception as e:
        XLogger.getInstance().logger.error(str(e))
        status = 1

    return status


def run_bzbzfy(p_config=None, p_db_conn_mpp=None, p_db_conn_rds=None, p_db_conn_dbprod7=None, p_account=None, p_month=None):
    BZBZFYJob(p_config=p_config,
               p_db_conn_mpp=p_db_conn_mpp,
               p_db_conn_rds=p_db_conn_rds,
               p_db_conn_dbprod7=p_db_conn_dbprod7,
               p_account=p_account,
               p_month=p_month).execute()



